<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="员工名称" sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" sortable>
			</el-table-column>
			<el-table-column prop="imgUrl" label="照片" sortable>
				<!-- 图片的显示 -->
				<!--<template   slot-scope="scope">
					<img :src="imgUrl"  min-width="70" height="70" />
				</template>-->
			</el-table-column>
			<el-table-column prop="roleName" label="角色" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="员工名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="照片" prop="imgUrl">
					<!--<el-input v-model="editForm.imgUrl" auto-complete="off"></el-input>-->
					<el-upload
							class="upload-demo"
							action="http://localhost/file/upload"
							:on-success="handleSuccessEdit"
							:file-list="fileList"
							list-type="picture">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item label="角色" prop="role_id">
					<el-select v-model="editForm.role_id" placeholder="请选择角色">
						<el-option
								v-for="item in options"
								:key="item.value"
								:label="item.label"
								:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="员工名称" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="addForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="addForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="照片" prop="imgUrl">
					<!--<el-input v-model="addForm.imgUrl" auto-complete="off"></el-input>-->
					<el-upload
							class="upload-demo"
							action="http://localhost/file/upload"
							:on-success="handleSuccessAdd"
							:file-list="fileList"
							list-type="picture">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item label="角色" prop="role_id">
					<el-select v-model="addForm.role_id" placeholder="请选择角色">
						<el-option
								v-for="item in roles"
								:key="item.id"
								:label="item.roleName"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

    export default {
        data() {
            return {
                fileList:[],
                //高级查询时传递的值
                filters:{
                    name:''
                },
                currentPage:1,
                total:0,
                sels: [],//列表选中列
                employees: [],
                listLoading: false,//如果为false，显示加载中效果，转圈圈
                editFormVisible: false,//编辑界面是否显示，false不显示
                //编辑界面数据,修改form对象
                editForm: {
                    id:0,
                    name:'',
                    email:'',
                    imgUrl:'',
                    role_id:''
                },
                editLoading: false,//修改加载
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' }
                    ],
                },
                addFormVisible: false,//新增界面是否显示
                //新增界面数据，修改form对象
                addForm: {
                    name:'',
                    email:'',
                    imgUrl:'',
                    role_id:''
                },
                addLoading: false,//新增加载
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]

                },
				roles:[],
                options: [{
                    value: '1',
                    label: '1'
                }, {
                    value: '2',
                    label: '2'
                },{
                    value: '3',
                    label: '3'
                }, {
                    value: '4',
                    label: '4'
                },{
                    value: '5',
                    label: '5'
                }, {
                    value: '6',
                    label: '6'
                },{
                    value: '7',
                    label: '7'
                }, {
                    value: '8',
                    label: '8'
                }]
            }
        },
        methods: {
            handleSuccessAdd(response, file, fileList){
                //后台上传成功之后，内容返回到该方法
                console.log(response);
                console.log(this.employees);
                //操作
                this.addForm.imgUrl = response;
                //提交的时候，取到图片的值 --把地址保存到数据库
                //定时器 不要图片
            },
            handleSuccessEdit(response, file, fileList){
                //后台上传成功之后，内容返回到该方法
                console.log(response);
                console.log(this.employees);
                //操作
                this.editForm.imgUrl = response;
                //提交的时候，取到图片的值 --把地址保存到数据库
                //定时器 不要图片
            },
            handleCurrentChange(val){
                //当前页
                this.currentPage = val;
                this.getEmployees();
            },
			getRoles(){
                this.$http.patch('role/list').then(res=>{
                    console.log(res.data)
                    this.roles = res.data;
                })
			},
            //获取员工列表
            getEmployees() {
                //发送axios请求到后端 查询员工
                // http://localhost:80/employee/list
                let param = {
                    name:this.filters.name,
                    currentPage: this.currentPage
                }
                this.$http.patch('employee/queryPage',param).then(res=>{
                    this.total = res.data.total;
                    this.employees = res.data.rows;
                })
            },
            //弹出新增界面
            handleAdd() {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    age: '',
                    email: ''
                };
            },
            //弹出编辑界面
            handleEdit(index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //删除
            handleDel(index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    //出现加载中效果
                    this.listLoading = true;
                    let id = row.id;
                    this.$http.delete('employee/'+id).then(res=>{
                        //去掉转圈圈效果
                        this.listLoading = false;
                        let {success,message} = res.data;
                        if(success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.getEmployees();
                        }else {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //新增
            addSubmit() {
                //提交之前的验证工作--elementui验证
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.addForm);
                            this.$http.put('employee/save',para).then((res) => {
                                //去掉转圈圈效果
                                this.addLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //清空表单
                                    this.$refs['addForm'].resetFields();
                                    //关闭对话框
                                    this.addFormVisible = false;
                                    this.getEmployees();
                                }else {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //修改提交
            editSubmit() {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('employee/update',para).then((res) => {
                                this.editLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getEmployees();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //多选方法
            selsChange: function (sels) {
                this.sels = sels;
            }
        },
        mounted() {
            this.getEmployees();
            this.getRoles();
        }
    }

</script>

<style scoped>

</style>